<?php

namespace App\Http\Controllers;

use App\Models\M3Result;
use App\Models\Tempphone;
use App\Tool\SMS\SendTemplateSMS;
use Illuminate\Http\Request;

class SmsController extends Controller
{
    public function sendSMS(Request $request){

        $m3_result = new M3Result();

        $phone = $request->input('phone','');

        if($phone == ''){
            $m3_result->status=1;
            $m3_result->message='手机号码不能为空';
            return $m3_result->toJson();
        }

        $sendTemplateSMS = new SendTemplateSMS();
        //自定义短信验证码的格式——6位数字
        $code ='';
        $charset = '1234567890';
        $_len = strlen($charset)-1;
        for ($i = 0;$i < 6;++$i){
            $code .= $charset[mt_rand(0,$_len)];
        }

        $m3_result = $sendTemplateSMS->sendTemplateSMS($phone,array($code,60),1);

        if($m3_result->status==0){
            $m3_result->message='短信发送成功!';

            $tempphone = new Tempphone();
            $tempphone->phone = $phone;
            $tempphone->code = $code;
            $tempphone->deadline = date('Y-m-d H-i-s',time()+60*60);
            $tempphone->save();
        }

        return $m3_result->toJson();
    }
}
